class Solution
{
public:
    bool arrayStringsAreEqual(vector<string> &word1, vector<string> &word2)
    {
        auto outerPtr1 = word1.begin();
        auto outerPtr2 = word2.begin();
        auto innerPtr1 = outerPtr1->begin();
        auto innerPtr2 = outerPtr2->begin();
        while (true)
        {
            if (innerPtr1 == outerPtr1->end())
            {
                ++outerPtr1;
                if (outerPtr1 == word1.end())
                {
                    return innerPtr2 == outerPtr2->end() && outerPtr2 + 1 == word2.end();
                }
                else
                {
                    innerPtr1 = outerPtr1->begin();
                }
            }
            if (innerPtr2 == outerPtr2->end())
            {
                ++outerPtr2;
                if (outerPtr2 == word2.end())
                {
                    return false;
                }
                else
                {
                    innerPtr2 = outerPtr2->begin();
                }
            }
            if (*innerPtr1 != *innerPtr2)
            {
                return false;
            }
            ++innerPtr1;
            ++innerPtr2;
        }
        return true;
    }
};